package com.lemeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lemeng.entity.Goods;
import com.lemeng.pojo.Category;
import com.lemeng.pojo.Sku;
import com.lemeng.pojo.Spu;
import com.lemeng.service.AdService;
import com.lemeng.service.CategoryService;
import com.lemeng.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("item")
public class ItemController {

    @Reference
    SpuService spuService;
    @Autowired
    TemplateEngine templateEngine;
    @Reference
    CategoryService categoryService;


    @GetMapping("createPage")
    public void index(String spuId){
        Goods goods = spuService.findById(spuId);
        List<Sku> skuList = goods.getSkuList();
        Spu spu = goods.getSpu();

        HashMap<String, String> map = new HashMap<>();
        for (Sku sku : skuList) {
            map.put(JSONObject.toJSONString(JSONObject.parseObject(sku.getSpec()), SerializerFeature.MapSortField),sku.getId()+".html");
        }

        for (Sku sku : skuList) {

            Context context = new Context();
            HashMap<String, Object> hashMap = new HashMap<>();

            hashMap.put("spu",spu);
            hashMap.put("sku",sku);
            hashMap.put("skuImages",sku.getImages().split(","));
            ArrayList<Category> categoriesArrayList = new ArrayList<>();
            categoriesArrayList.add(categoryService.findById(spu.getCategory1Id()));
            categoriesArrayList.add(categoryService.findById(spu.getCategory2Id()));
            categoriesArrayList.add(categoryService.findById(spu.getCategory3Id()));
            hashMap.put("categoriesList",categoriesArrayList);


            hashMap.put("paraItems", JSONObject.parseObject(spu.getParaItems()));
            hashMap.put("specItems", JSONObject.parseObject(sku.getSpec()));

            //循环展示可选项

            Map<String, Object> innerMap = JSONObject.parseObject(spu.getSpecItems()).getInnerMap();

            for (String s : innerMap.keySet()) {
                List<String> list = (List<String>) innerMap.get(s);

                ArrayList<Map> listmap = new ArrayList<>();

                for (String s1 : list) {
                    HashMap<String, Object> hashMap1 = new HashMap<>();
                    hashMap1.put("option",s1);

                    Map<String, Object> skuSpec = JSONObject.parseObject(sku.getSpec()).getInnerMap();
                    if (skuSpec.get(s).toString().equals(s1)) {
                        hashMap1.put("checked",true);
                    }else {
                        hashMap1.put("checked",false);
                    }

                    JSONObject spec = JSONObject.parseObject(sku.getSpec());
                    spec.put(s,s1);
                    String url = map.get(JSONObject.toJSONString(spec,SerializerFeature.MapSortField));
                    hashMap1.put("url",url);

                    listmap.add(hashMap1);
                }
                innerMap.put(s,listmap);
            }
            hashMap.put("spec", innerMap);

            context.setVariables(hashMap);

            try {
                templateEngine.process("item",context,new PrintWriter(new File("D:\\zhuomian\\P9\\day01-thymeleaf使用以及分类信息+轮播图服务模块渲染\\资料\\资料\\网站前台\\"+sku.getId()+".html")));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

    }


}
